/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getOrderRecordList,
  getDownloadTaskList,
  getStatisticsList,
  getStatisticsDetailList,
  addDownLoadTask,
  getOrderRecordDetail,
  addDownLoad,
  getDownloadList,
  getWsRecordList,
  getWsStatisticsList,
  getWsOrderDetail,
  wsAddDownLoad,
  wsGetDownloadList,
  getYZRecordList,
  getYZStatisticsList,
  yzGetOrderDetail,
} from '@/services/orderManagement';
import { message } from 'antd';
import { isLegalData } from '@/utils/utils';
import { isArray } from 'lodash';

export default {
  namespace: 'orderRecord',

  state: {
    orderRecordList: {
      list: null,
      pagination: null,
    },
    downLoadTask: {
      list: null,
      pagination: null,
    },
    downLoadList: {
      // 单号使用统计下载任务列表
      list: null,
      pagination: null,
    },
    statisticsList: {
      list: null,
      pagination: null,
    },
    statisticsDetailList: {
      list: null,
      pagination: null,
    },
    orderDetailList: {},
    wsOrderDetail: {},
    yzOrderDetail: {},
  },

  effects: {
    // 单号记录，获取列表
    *getOrderRecordList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOrderRecordList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, page, totalCount, pageSize } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            orderRecordList: {
              list: isArray(list) ? list : [],
              pagination: {
                total: totalCount,
                current: page,
                pageSize,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号记录，添加下载任务
    *addDownLoadTask({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addDownLoadTask, payload);

      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success('添加成功！');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号记录，查看下载任务
    *getDownloadTaskList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDownloadTaskList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, totalCount, page, pageSize } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            downLoadTask: {
              list: isArray(list) ? list : [],
              pagination: {
                total: totalCount,
                current: page,
                pageSize,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号使用统计，添加下载任务
    *addDownLoad({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addDownLoad, payload);

      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success('添加成功！');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号使用统计，查看下载任务
    *getDownloadList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDownloadList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const list = isArray(data.data) ? data.data : [];
      const { total } = data;
      const { page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            downLoadList: {
              list,
              pagination: {
                total,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商，单号使用统计，添加下载任务
    *wsAddDownLoad({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wsAddDownLoad, payload);

      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success('添加成功！');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商，单号使用统计，查看下载任务
    *wsGetDownloadList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(wsGetDownloadList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const list = isArray(data.list) ? data.list : [];
      const { totalCount, pageSize } = data;
      const { page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            downLoadList: {
              list,
              pagination: {
                total: totalCount,
                current: page,
                pageSize,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号使用统计列表
    *getStatisticsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getStatisticsList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const list = isArray(data.data) ? data.data : [];
      const total = data.count;
      const { page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            statisticsList: {
              list,
              pagination: {
                total,
                current: page,
                pageSize: 100,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getStatisticsDetailList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getStatisticsDetailList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, total, page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            statisticsDetailList: {
              list: isArray(list) ? list : [],
              pagination: {
                total,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号记录，订单详情
    *getOrderRecordDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOrderRecordDetail, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            orderDetailList: data || {},
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商单号记录，获取列表
    *getWsRecordList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWsRecordList, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { total, page, list, page_size } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            orderRecordList: {
              list: isArray(list) ? list : [],
              pagination: {
                total,
                current: page,
                pageSize: page_size,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商单号使用统计，获取列表
    *getWsStatisticsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWsStatisticsList, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            statisticsList: {
              list: isArray(data) ? data : [],
              pagination: false,
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商单号记录，查看详情
    *getWsOrderDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWsOrderDetail, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            wsOrderDetail: data || {},
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 驿站单号记录，获取列表
    *getYZRecordList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getYZRecordList, { ...payload, pageSize: 100 });
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { page, totalCount, list } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            orderRecordList: {
              list: isLegalData(list),
              pagination: {
                total: totalCount,
                current: page,
                pageSize: 100,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 驿站单号使用统计，获取列表
    *getYZStatisticsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getYZStatisticsList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const list = isArray(data.data) ? data.data : [];
      const total = data.count;
      const { page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            statisticsList: {
              list,
              pagination: {
                total,
                current: page,
                pageSize: 100,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 驿站单号记录，查看详情
    *yzGetOrderDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(yzGetOrderDetail, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            yzOrderDetail: data || {},
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
